import org.junit.Assert;
import org.junit.Test;

import java.util.Set;
import java.util.TreeSet;

import static org.junit.Assert.*;

public class PointSETTest {

    @Test
    public void testConstructorInitializesEmptySet() {
        PointSET pointSet = new PointSET();
        assertTrue(pointSet.isEmpty());
        assertEquals(0, pointSet.size());

    }

    @Test
    public void testInsertPoint() {
        PointSET pointSet = new PointSET();
        Point2D p = new Point2D(0, 0);
        pointSet.insert(p);
        assertFalse(pointSet.isEmpty());
        assertEquals(1, pointSet.size());
        assertTrue(pointSet.contains(p));

    }

    @Test
    public void testInsertIfNotThere() {
        PointSET pointSet = new PointSET();
        pointSet.insert(new Point2D(0, 0));
        pointSet.insert(new Point2D(0, 0));
        assertEquals(1, pointSet.size());
    }

    @Test
    public void nearest() {
        PointSET pointSet = new PointSET();
        pointSet.insert(new Point2D(1, 0));
        pointSet.insert(new Point2D(0, 10));
        pointSet.insert(new Point2D(10, 0));
        pointSet.insert(new Point2D(0, 5));
        pointSet.insert(new Point2D(5, 0));

        assertEquals(new Point2D(1, 0), pointSet.nearest(new Point2D(0, 0)));

    }

    @Test
    public void nearestNegative() {
        PointSET pointSet = new PointSET();
        pointSet.insert(new Point2D(-1, 0));
        pointSet.insert(new Point2D(0, 10));
        pointSet.insert(new Point2D(10, 0));
        pointSet.insert(new Point2D(0, 5));
        pointSet.insert(new Point2D(5, 0));

        assertEquals(new Point2D(-1, 0), pointSet.nearest(new Point2D(0, 0)));

    }


    @Test
    public void insideRectangle() {
        PointSET pointSet = new PointSET();
        pointSet.insert(new Point2D(1, 0));
        pointSet.insert(new Point2D(0, 10));
        pointSet.insert(new Point2D(10, 0));
        pointSet.insert(new Point2D(0, 5));
        pointSet.insert(new Point2D(5, 0));

        RectHV rect = new RectHV(0, 0, 3, 3);

        Set<Point2D> pointSetInsideRectangle = new TreeSet<Point2D>();
        pointSetInsideRectangle.add(new Point2D(1, 0));

        assertEquals(pointSetInsideRectangle, pointSet.range(rect));

    }


    @Test
    public void insideRectNone() {
        PointSET pointSet = new PointSET();
        pointSet.insert(new Point2D(4, 0));
        pointSet.insert(new Point2D(0, 10));
        pointSet.insert(new Point2D(10, 0));
        pointSet.insert(new Point2D(0, 5));
        pointSet.insert(new Point2D(5, 0));

        RectHV rect = new RectHV(0, 0, 3, 3);

        Set<Point2D> pointSetInsideRectangle = new TreeSet<Point2D>();

        assertEquals(pointSetInsideRectangle, pointSet.range(rect));

    }

}